package com.cnplan.filter;

import com.cnplan.core.Constant;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by Administrator on 2017/8/24.
 */
public class MvcInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String fromUrl = httpServletRequest.getHeader("Referer");

        if (httpServletRequest.getSession().getAttribute(Constant.FROM_URL) == null) {
            httpServletRequest.getSession().setAttribute(Constant.FROM_URL, fromUrl);
        } else {
            if (fromUrl!=null && !fromUrl.contains(httpServletRequest.getRequestURI())) {
                httpServletRequest.getSession().setAttribute(Constant.FROM_URL, fromUrl);
            }
        }
        httpServletRequest.getSession().setAttribute(Constant.CURRENT_URL, httpServletRequest.getServletPath());
        return true;
    }


    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
